<?php
    // +----------------------------------------------------------------------
    // | 海豚PHP框架 [ DolphinPHP ]
    // +----------------------------------------------------------------------
    // | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
    // +----------------------------------------------------------------------
    // | 官方网站: http://dolphinphp.com
    // +----------------------------------------------------------------------
    // | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
    // +----------------------------------------------------------------------

    namespace app\payment\validate;

    use app\member\model\MemberModel;
    use app\payment\model\PaymentInfoModel;
    use app\payment\model\PaymentWithdrawModel;
    use app\payment\model\WalletModel;
    use think\Cache;
    use think\Validate;

    /**
     *
     * @package app\wechat\validate
     * @author  蔡伟明 <314013107@qq.com>
     */
    class Withdraw extends Validate
    {

        // 定义验证规则
        protected $rule = [
            'money|提现金额' => 'require|number|min:3|checkMoney',
            'type|提现方式' => 'require|number|in:1,2,3|checkType',
            'wxpay_name|微信真实姓名' => 'checkWxpay|min:1',
            'wxpay_account|微信账号' => 'checkWxpay|min:1',
            'aliplay_name|支付宝真实姓名' => 'min:1',
            'aliplay_account|支付宝真实姓名' => 'min:1',
            'bank_name|银行名称' => 'min:1',
            'bank_card_number|银行卡号' => 'number|min:1',
            'bank_ture_name|持卡人姓名' => 'min:1',
            'order_sn|提现订单号' => 'require|checkOrderSN',
            'validate_code|验证码' => 'require|number|length:6,6|checkValidateCode',
        ];

        // 定义验证提示
        protected $message = [
            'money.min' => '提现金额不得小于100',
            'type.in' => '提现只能是支付宝微信或者银行卡',
        ];

        // 定义验证场景
        protected $scene = [
            'set_withdraw_info' => [
                'validate_code',
                'wxpay_name',
                'wxpay_account',
                'aliplay_name',
                'aliplay_account',
                'bank_name',
                'bank_card_number',
                'bank_ture_name',
            ],
            'addwithdraw' => ['money', 'type'],
            'backout' => ['order_sn'],
        ];

        /**
         * 验证短信是否正确
         * @param $rule
         * @param $value
         * @param $data
         * @return bool|string
         */
        protected function checkValidateCode($rule, $value, $data)
        {
            return Cache::get('timephone_' . $data['phone']) == $data['validate_code'] ? true : '验证码不正确';
        }

        /**
         * 检查是否可以撤单
         * @param $rule
         * @param $value
         * @param $data
         * @return bool|string
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         */
        protected function checkOrderSN($rule, $value, $data)
        {

            $order_ = PaymentWithdrawModel::where(['order_sn' => $data['order_sn']])->find();
            if (!in_array($order_['status'], [0])) {
                return '提现已经被受理不能撤单！';
            }
            return true;
        }

        /**
         * @param $rule
         * @param $value
         * @param $data
         *
         * @return bool|string
         */
        protected function checkWxpay($rule, $value, $data)
        {

            if (!isset($data['wxpay_name'])) {
                return '微信真实姓名不能为空';
            }
            if (!isset($data['wxpay_account'])) {
                return '微信账号不能为空';
            }
            return true;

        }

        /**
         * 检查资金是否足够
         * @param $rule
         * @param $value
         * @param $data
         * @return bool|string
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         */
        protected function checkMoney($rule, $value, $data)
        {

            $Wallet = WalletModel::where(['member_id' => $data['member_id']])->find();
            if ($data['money'] < 100) {
                return '提现金额不能小于100';
            }
            return (($Wallet['money'] / 100) >= $data['money']) ? true : '提现金额不足，请重新输入！';
        }

        /**
         * 检查是否绑定账号
         * @param $rule
         * @param $value
         * @param $data
         * @return bool|string
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         */

        protected function checkType($rule, $value, $data)
        {

            $PaymentInfo = PaymentInfoModel::where(['member_id' => $data['member_id']])->find();

            switch ($data['type']) {
                case 1:
                    if ($PaymentInfo['wxpay_name'] == null || $PaymentInfo['wxpay_account'] == null) {
                        return '微信提现信息不完整！请补充后再提现！';
                    }

                    break;
                case 2:

                    if ($PaymentInfo['aliplay_name'] == null || $PaymentInfo['aliplay_account'] == null) {
                        return '支付宝提现信息不完整！请补充后再提现！';
                    }
                    break;
                case 3:

                    if ($PaymentInfo['bank_name'] == null || $PaymentInfo['bank_card_number'] == null || $PaymentInfo['bank_ture_name'] == null) {
                        return '银行卡提现信息不完整！请补充后再提现！';
                    }
                    break;
            }
            return true;
        }

    }
